//
//  UIView+ext.swift
//  CosClub
//
//  Created by 沙睿 on 2020/2/6.
//  Copyright © 2020 ios. All rights reserved.
//

import Foundation
import UIKit
extension UIView {
    public var x : CGFloat {
        set {
            let frame = CGRect.init(x: newValue,
                                    y: self.frame.origin.y,
                                    width: self.frame.size.width,
                                    height: self.frame.size.height)
            self.frame = frame
        }
        get {
            self.frame.origin.x;
        }
    }
    
    public var y : CGFloat {
        set {
            let frame = CGRect.init(x: self.frame.origin.x,
                                    y: newValue,
                                    width: self.frame.size.width,
                                    height: self.frame.size.height)
            self.frame = frame
        }
        get {
            self.frame.origin.y;
        }
    }
    
    public var width : CGFloat {
        set {
            let frame = CGRect.init(x: self.frame.origin.x,
                                    y: self.frame.origin.y,
                                    width: newValue,
                                    height: self.frame.size.height)
            self.frame = frame
        }
        get {
            self.frame.size.width;
        }
    }
    
    public var height : CGFloat {
        set {
            let frame = CGRect.init(x: self.frame.origin.x,
                                    y: self.frame.origin.y,
                                    width: self.frame.size.width,
                                    height: newValue)
            self.frame = frame
        }
        get {
            self.frame.size.height;
        }
    }
}



extension UIView {
    
    public enum ConType {
        case line
        case normal
    }
    
    public var sr_cornerRadius: CGFloat {
        set {
            guard !CGRect.zero.equalTo(self.frame) else {
                return
            }
            let cornet = UIRectCorner.allCorners;
            let path = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: cornet, cornerRadii: CGSize.init(width: newValue, height: newValue))
            let maskLayer = CAShapeLayer()
            
            maskLayer.frame = self.bounds;
            maskLayer.path = path.cgPath;
            self.layer.mask = maskLayer;
            
        }
        get {
            return 0
        }
    }
    
    public enum GradientDirection {
        case leftRight
        case topDown
    }
    
    /// 渐变颜色
    public func gradientColor(from: UIColor, to: UIColor, direction: GradientDirection = .topDown) {
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [from.cgColor, to.cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = self.bounds
        if direction == .topDown {
            bgLayer1.startPoint = CGPoint(x: 0.5, y: 0)
            bgLayer1.endPoint = CGPoint(x: 0.5, y: 1)
        }else {
            bgLayer1.startPoint = CGPoint(x: 0, y: 0.5)
            bgLayer1.endPoint = CGPoint(x: 1, y: 0.5)
        }
        
        self.layer.addSublayer(bgLayer1)
    }
    
    /// 渐变颜色
    public func gradientColor(from: UIColor, to: UIColor, start:CGPoint, endPoint:CGPoint) {
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [from.cgColor, to.cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = self.bounds
        bgLayer1.startPoint = start
        bgLayer1.endPoint = endPoint
        self.layer.insertSublayer(bgLayer1, at: 0)
    }
    
    public convenience init(type: ConType) {
        self.init()
        switch type {
        case .line:
            backgroundColor = "FAFAFA".color()
        default:
            backgroundColor = UIColor.white
        }
    }
}

extension UIView {
    
    /// 读取和view相同名称的nib
    /// - Returns: NibView
    public func loadNib() -> Self {
        let bundle = Bundle(for: type(of: self));
        let nibName = type(of: self).description().components(separatedBy: ".").last!
        let nib = UINib(nibName: nibName, bundle: bundle)
        return nib.instantiate(withOwner: self, options: nil).first as! Self
    }
}




